#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace GameFramework.GameComponents
{

    public enum FPSOutput
    {
        Debug,
        WindowTitle,
        Game
    };

    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class FPSCounter : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private FPSOutput outputFormat;

        public FPSOutput OutputFormat
        {
            get { return outputFormat; }
        }
        /// <summary>
        /// Constructor for the FPSCounter Game Component, this
        /// Will allow you to specify the initial settings for the control
        /// </summary>
        /// <param name="game">Base Game Class</param>
        /// <param name="FPSOutputFormat">Output Format for the Display of the FPS Data</param>
        public FPSCounter(Game game, FPSOutput FPSOutputFormat)
            : base(game)
        {
            // TODO: Construct any child components here
            content = new ContentManager(game.Services);
            this.outputFormat = FPSOutputFormat;
        }

        ContentManager content;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;

        int frameRate = 0;
        int frameCounter = 0;
        TimeSpan elapsedTime = TimeSpan.Zero;

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                spriteBatch = new SpriteBatch(GraphicsDevice);
                spriteFont = content.Load<SpriteFont>(@"Content\Fonts\FPSFont");
            }
        }


        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
                content.Unload();
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                frameRate = frameCounter;
                frameCounter = 0;
            }


            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            frameCounter++;

            string fps = string.Format("fps: {0}", frameRate);

            if (this.outputFormat == FPSOutput.Game)
            {
                spriteBatch.Begin();

                spriteBatch.DrawString(spriteFont, fps, new Vector2(33, 33), Color.Black);
                spriteBatch.DrawString(spriteFont, fps, new Vector2(32, 32), Color.White);

                spriteBatch.End();
            }
            if (this.outputFormat == FPSOutput.Debug)
            {
                System.Diagnostics.Debug.WriteLine(fps);
            }
            if (this.outputFormat == FPSOutput.WindowTitle)
            {
                base.Game.Window.Title = fps;
            }
        }

    }
}


